import xadmin

from xadmin import views

from . import models

class BaseSettings(object):
    """
    xadmin的基本配置
    """
    # 开启主题切换
    enable_themes = True
    use_bootswatch = True

class GlobalSettings(object):
    """
    xadmin全局配置
    """
    site_title = '美多商城管理系统'
    site_footer = '美多商城集团有限公司'
    menu_style = 'accordion' # 设置菜单折叠


# 注册模型类
# 定义管理类,继承object即可
class GoodsCategoryXadmin(object):
    # 设置图标
    model_icon = 'fa fa-gift'
    # 设置显示的字段
    list_display = ['id', 'name', 'parent']
    # 搜索字段
    search_fields = ['id', 'name']

    # 过滤字段, 过滤器
    list_filter = ['name']
    # 编辑字段
    list_editable = ['name']
    # 展示关联模型类的详细信息
    show_detail_fields = ['name']

    # 定时刷新, 可选刷新的秒数,在xadmin界面选择
    refresh_times = [3, 5, 100]

    # 统计图
    data_charts = {
        'name': {
            'title': '分类信息',
            'x-field': 'create_time',
            'y_field': ('id',),
        },
    }

    # 只读
    # readonly_fields = ['name']

    # 定义方法,在修改模型的时候执行
    def save_models(self):
        # 保存数据对象
        obj = self.new_obj
        obj.save()
        # 补充自定义行为
        from celery_tasks.html.tasks import generate_list_category_html
        generate_list_category_html.delay()

    def delete_model(self):
        # 删除数据对象
        obj = self.obj
        obj.delete()

        from celery_tasks.html.tasks import generate_list_category_html
        generate_list_category_html.delay()


xadmin.site.register(views.BaseAdminView, BaseSettings)
xadmin.site.register(views.CommAdminView, GlobalSettings)

xadmin.site.register(models.Goods)
xadmin.site.register(models.GoodsCategory, GoodsCategoryXadmin)
xadmin.site.register(models.GoodsSpecification)
xadmin.site.register(models.GoodsChannel)
xadmin.site.register(models.Brand)
xadmin.site.register(models.SpecificationOption)
xadmin.site.register(models.SKU)
xadmin.site.register(models.SKUSpecification)
xadmin.site.register(models.SKUImage)